﻿Public Partial Class New_ServicePopup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not (Session("Booking_NO_VALUE") Is Nothing) Then
                ViewState("EditBooking") = Session("Booking_NO_VALUE").ToString.Split("$")(0)
                txtWater.Text = Session("Booking_NO_VALUE").ToString.Split("$")(1)
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Session("Booking_NO_VALUE") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalNew_Service('2');", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim Water As Integer = 0
        Dim Tug As Integer = 0
        If (txtWater.Text <> Session("Booking_NO_VALUE").ToString.Split("$")(1)) Then
            Water = txtWater.Text
        End If
        If (txtTug.Text > 0) Then
            Tug = txtTug.Text
        End If
        If (Water = "0") And (Tug = "0") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ไม่พบรายการขอเพิ่มบริการ');", True)
            Exit Sub
        End If

        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        Dim A_Code As String = clsDB.GeneratorID(conn, "Action_Operation")
        tran = clsDB.BeginTran(conn)

        If (Water <> "0") Then
            Dim wp As String = "N"
            If (Session("Booking_NO_VALUE").ToString.Split("$")(1) <> "0") Then
                wp = "O"
            End If
            If (clsFc.InsertAdd_Tariff_New(ViewState("EditBooking"), "WP", Water, "O", "N", "", "", "", txtRemark.Text, conn, tran) < 1) Then
                condition = False
            End If
        End If
        If (Tug <> "0") Then
            If (clsFc.InsertAdd_Tariff_New(ViewState("EditBooking"), "000000002100000236", Tug, "N", "N", "", "", "", txtRemark.Text, conn, tran) < 1) Then
                condition = False
            End If
        End If

        If (condition) Then
            clsDB.CommitTran(tran)
            Session("Booking_NO_VALUE") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalNew_Service('1');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()

    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class